﻿using System;
using System.Collections.Generic;
using System.Text;
using AutoShutdownDotNet.DataAccess;

namespace AutoShutdownDotNet.Models
{



    public class Task
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public DateTime? When { get; set; }
        public int Hour { get; set; }
        public int Minute { get; set; }
        public Recurrences Recurrence { get; set; }
        public bool AskBefore { get; set; }
        public AutoShutdownAction Action { get; set; }
        public int Timer { get; set; }
        public bool Active { get; set; }
        public string Message { get; set; }
        public string FilePath { get; set; }
        public string Comment { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime LastModified { get; set; }

        public string Time
        {
            get
            {
                string h = Hour.ToString();
                if (h.Length == 1)
                    h = "0" + h;
                string m = Minute.ToString();
                if (m.Length == 1)
                    m = "0" + m;
                return h + ":" + m;
            }
        }

        public string WhenToString()
        {
            if (When.HasValue)
                return When.Value.ToShortDateString();
            return RecurrenceToString(this.Recurrence);
        }

        public static string WhenToString(DateTime? when, Recurrences recurrences)
        {
            if(when.HasValue)
                return when.Value.ToShortDateString();
            return RecurrenceToString(recurrences);
        }

        public static string TimeToString(int hour, int minute)
        {
            string h = hour.ToString();
            if (h.Length == 1)
                h = "0" + h;
            string m = minute.ToString();
            if (m.Length == 1)
                m = "0" + m;
            return h + ":" + m;
        }

        public static string RecurrenceToString(Recurrences recurrences)
        {
            switch (recurrences)
            {
                case Recurrences.Everyday:
                    return "Everyday";
                case Recurrences.Mondays:
                    return "Every Monday";
                case Recurrences.Saturdays:
                    return "Every Saturday";
                case Recurrences.Sundays:
                    return "Every Sunday";
                case Recurrences.Thursdays:
                    return "Every Thursday";
                case Recurrences.Tuesdays:
                    return "Every Tuesday";
                case Recurrences.Wendsdays:
                    return "Every Wendsday";
                default:
                    return "Everyday";
            }
        }

        public static string ActionToString(AutoShutdownAction action)
        {
            switch (action)
            {
                case AutoShutdownAction.Hibernate:
                    return "Hibernate";
                case AutoShutdownAction.Logoff:
                    return "Logoff";
                case AutoShutdownAction.PowerOff:
                    return "PowerOff";
                case AutoShutdownAction.Reboot:
                    return "Reboot";
                case AutoShutdownAction.RunApplication:
                    return "Run Application";
                case AutoShutdownAction.ShowMessage:
                    return "Show Message";
                case AutoShutdownAction.Standby:
                    return "Standby";
                case AutoShutdownAction.Lock:
                    return "Lock";
                default:
                    return "Undefined";
            }
        }

        public static bool CompareDay(DayOfWeek dayOfWeek, Recurrences recurrence)
        {
            if (dayOfWeek == DayOfWeek.Friday && recurrence == Recurrences.Fridays)
                return true;
            if (dayOfWeek == DayOfWeek.Monday && recurrence == Recurrences.Mondays)
                return true;
            if (dayOfWeek == DayOfWeek.Saturday && recurrence == Recurrences.Saturdays)
                return true;
            if (dayOfWeek == DayOfWeek.Sunday && recurrence == Recurrences.Sundays)
                return true;
            if (dayOfWeek == DayOfWeek.Thursday && recurrence == Recurrences.Thursdays)
                return true;
            if (dayOfWeek == DayOfWeek.Tuesday && recurrence == Recurrences.Tuesdays)
                return true;
            if (dayOfWeek == DayOfWeek.Wednesday && recurrence == Recurrences.Wendsdays)
                return true;
            return false;
        }

        public void Insert()
        {
            CreatedDate = DateTime.Now;
            Database db = new Database();
            db.Insert(this);
            Id = db.GetLastTaskId();
        }

        public void Update()
        {
            if(Id > 0)
            {
                LastModified = DateTime.Now;
                Database db = new Database();
                db.Update(this);
            }
            else
            {
                throw new Exception("You need to pass a valid TaskId for update this object.");
            }
        }

        public void Delete()
        {
            if (Id > 0)
            {
                Database db = new Database();
                db.Delete(this);
            }
            else
            {
                throw new Exception("You need to pass a valid TaskId for update this object.");
            }
        }

        public bool Exist()
        {
            Database db = new Database();
            return db.Exist(Id,When, Hour, Minute,Recurrence);
        }

        public static List<Task> Select(DateTime? from, DateTime? to)
        {
            return new Database().SelectAllTasks(from, to);
        }

        public static Task Load(int id)
        {
            return new Database().Load(id);
        }

        public static bool Exist(int taskId,DateTime? when, int hour, int minute, Recurrences recurrences)
        {
            Database db = new Database();
            return db.Exist(taskId,when, hour, minute,recurrences);
        }
    }
}
